package com.ruoyi.web.controller.project;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.project.domain.entity.ProjectSaleConfig;
import com.ruoyi.project.service.IProjectSaleConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收款出库单关联Controller
 *
 * @author zzy
 * @date 2025-11-27
 */
@RestController
@RequestMapping("/project/config")
public class ProjectSaleConfigController extends BaseController {
    @Autowired
    private IProjectSaleConfigService projectSaleConfigService;

    /**
     * 查询收款出库单关联列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProjectSaleConfig projectSaleConfig) {
        startPage();
        List<ProjectSaleConfig> list = projectSaleConfigService.selectProjectSaleConfigList(projectSaleConfig);
        return getDataTable(list);
    }

    /**
     * 导出收款出库单关联列表
     */
    @Log(title = "收款出库单关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectSaleConfig projectSaleConfig) {
        List<ProjectSaleConfig> list = projectSaleConfigService.selectProjectSaleConfigList(projectSaleConfig);
        ExcelUtil<ProjectSaleConfig> util = new ExcelUtil<ProjectSaleConfig>(ProjectSaleConfig.class);
        util.exportExcel(response, list, "收款出库单关联数据");
    }

    /**
     * 获取收款出库单关联详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(projectSaleConfigService.selectProjectSaleConfigById(id));
    }

    @GetMapping(value = "/feeKey/{feeKey}")
    public R feeKey(@PathVariable("feeKey") String feeKey) {
        if (feeKey == null || feeKey.equals("")) {
            throw new RuntimeException("服务器出错!");
        }

        try {
            LambdaQueryWrapper<ProjectSaleConfig> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProjectSaleConfig::getFeeKey, feeKey);
            ProjectSaleConfig one = projectSaleConfigService.getOne(queryWrapper);
            if (one == null) {
                return R.ok(new ProjectSaleConfig());
            }
            return R.ok(one);
        } catch (Exception e) {
            throw new RuntimeException("服务器错误");
        }

    }



    /**
     * 新增收款出库单关联
     */
    @Log(title = "收款出库单关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProjectSaleConfig projectSaleConfig) {
        return toAjax(projectSaleConfigService.insertProjectSaleConfig(projectSaleConfig));
    }

    /**
     * 修改收款出库单关联
     */
    @Log(title = "收款出库单关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectSaleConfig projectSaleConfig) {
        return toAjax(projectSaleConfigService.updateProjectSaleConfig(projectSaleConfig));
    }

    /**
     * 修改收款出库单关联
     */
    @Log(title = "收款出库单关联", businessType = BusinessType.UPDATE)
    @PutMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody ProjectSaleConfig projectSaleConfig) {
        //先查询
        try {
            LambdaQueryWrapper<ProjectSaleConfig> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProjectSaleConfig::getFeeKey, projectSaleConfig.getFeeKey());
            ProjectSaleConfig one = projectSaleConfigService.getOne(queryWrapper);
            if (one != null) {
                one.setFeeKey(projectSaleConfig.getFeeKey());
                one.setSaleKey(projectSaleConfig.getSaleKey());
                projectSaleConfigService.updateById(one);
            }else{
                projectSaleConfigService.save(projectSaleConfig);
            }

        } catch (Exception e) {
            throw new RuntimeException("服务器出错,请联系管理员");
        }
        return R.ok();


    }


    /**
     * 删除收款出库单关联
     */
    @Log(title = "收款出库单关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(projectSaleConfigService.deleteProjectSaleConfigByIds(ids));
    }
}
